
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 11/10/2011 17:13:58
-- Generated from EDMX file: E:\Nguyen Quang Trung\Do an tot nghiep\Nguyen Quang Trung - D07CN1\Social_Photos\Code\SocialPhotos\DataModel\SocialPhotoModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [SocialPhoto];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_CategoryPhoto_Category]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CategoryPhoto] DROP CONSTRAINT [FK_CategoryPhoto_Category];
GO
IF OBJECT_ID(N'[dbo].[FK_CategoryPhoto_Photo]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CategoryPhoto] DROP CONSTRAINT [FK_CategoryPhoto_Photo];
GO
IF OBJECT_ID(N'[dbo].[FK_UserPhoto]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Photos] DROP CONSTRAINT [FK_UserPhoto];
GO
IF OBJECT_ID(N'[dbo].[FK_UserComment]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Comments] DROP CONSTRAINT [FK_UserComment];
GO
IF OBJECT_ID(N'[dbo].[FK_PhotoComment]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Comments] DROP CONSTRAINT [FK_PhotoComment];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Photos]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Photos];
GO
IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO
IF OBJECT_ID(N'[dbo].[Comments]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Comments];
GO
IF OBJECT_ID(N'[dbo].[Categories]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Categories];
GO
IF OBJECT_ID(N'[dbo].[CategoryPhoto]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CategoryPhoto];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Photos'
CREATE TABLE [dbo].[Photos] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [OriginalFileName] nvarchar(max)  NULL,
    [Likes] smallint  NULL,
    [Dislikes] smallint  NULL,
    [ContentType] nvarchar(max)  NOT NULL,
    [Height] nvarchar(max)  NULL,
    [Width] nvarchar(max)  NULL,
    [Title] nvarchar(max)  NULL,
    [FileName] nvarchar(max)  NOT NULL,
    [UploadTime] datetime  NULL,
    [Longitude] float  NULL,
    [Latitude] float  NULL,
    [PublicLevel] smallint  NULL,
    [User_Id] int  NOT NULL
);
GO

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Password] nvarchar(max)  NOT NULL,
    [ProviderKey] nvarchar(max)  NULL
);
GO

-- Creating table 'Comments'
CREATE TABLE [dbo].[Comments] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Content] nvarchar(max)  NOT NULL,
    [PostedTime] datetime  NOT NULL,
    [User_Id] int  NOT NULL,
    [Photo_Id] int  NOT NULL
);
GO

-- Creating table 'Categories'
CREATE TABLE [dbo].[Categories] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'CategoryPhoto'
CREATE TABLE [dbo].[CategoryPhoto] (
    [Category_Id] int  NOT NULL,
    [Photos_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Photos'
ALTER TABLE [dbo].[Photos]
ADD CONSTRAINT [PK_Photos]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Comments'
ALTER TABLE [dbo].[Comments]
ADD CONSTRAINT [PK_Comments]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Categories'
ALTER TABLE [dbo].[Categories]
ADD CONSTRAINT [PK_Categories]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Category_Id], [Photos_Id] in table 'CategoryPhoto'
ALTER TABLE [dbo].[CategoryPhoto]
ADD CONSTRAINT [PK_CategoryPhoto]
    PRIMARY KEY NONCLUSTERED ([Category_Id], [Photos_Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Category_Id] in table 'CategoryPhoto'
ALTER TABLE [dbo].[CategoryPhoto]
ADD CONSTRAINT [FK_CategoryPhoto_Category]
    FOREIGN KEY ([Category_Id])
    REFERENCES [dbo].[Categories]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Photos_Id] in table 'CategoryPhoto'
ALTER TABLE [dbo].[CategoryPhoto]
ADD CONSTRAINT [FK_CategoryPhoto_Photo]
    FOREIGN KEY ([Photos_Id])
    REFERENCES [dbo].[Photos]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CategoryPhoto_Photo'
CREATE INDEX [IX_FK_CategoryPhoto_Photo]
ON [dbo].[CategoryPhoto]
    ([Photos_Id]);
GO

-- Creating foreign key on [User_Id] in table 'Photos'
ALTER TABLE [dbo].[Photos]
ADD CONSTRAINT [FK_UserPhoto]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserPhoto'
CREATE INDEX [IX_FK_UserPhoto]
ON [dbo].[Photos]
    ([User_Id]);
GO

-- Creating foreign key on [User_Id] in table 'Comments'
ALTER TABLE [dbo].[Comments]
ADD CONSTRAINT [FK_UserComment]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserComment'
CREATE INDEX [IX_FK_UserComment]
ON [dbo].[Comments]
    ([User_Id]);
GO

-- Creating foreign key on [Photo_Id] in table 'Comments'
ALTER TABLE [dbo].[Comments]
ADD CONSTRAINT [FK_PhotoComment]
    FOREIGN KEY ([Photo_Id])
    REFERENCES [dbo].[Photos]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PhotoComment'
CREATE INDEX [IX_FK_PhotoComment]
ON [dbo].[Comments]
    ([Photo_Id]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------